<?php
namespace app\member\controller;
/**
 * #素材管理#
 */
class Source extends Common{
	/**
	 * #素材列表#
	 * @return view
	 */
	function index(){
		$uid=$this->user['id'];
		$cate=controller('category')->getCates();
		$params=[
			'cate_id'=>input('cate_id/d',isset($cate[0])?$cate[0]['id']:0),
			'keyword'=>input('keyword/s',''),
		];
		$where=['uid'=>$uid];
		$options=[
			'with'=>'media',
		];
		
		if($params['cate_id']>0){
			$where['cate_id']=$params['cate_id'];
		}
		if($params['keyword']){
			$where['title']=['title','like',"'%".$params['keyword']."%'"];
		}
		$list=model($this->table)->readAll($where,true,$options);
		$list->appends($params);
		foreach ($list as $k => $v) {
			if(isset($v['map'])){
				$list[$k]['map']=json_decode($v['map'],true);
			}
		}
		$this->assign('list',$list);
		$this->assign('params',$params);
		
		$this->assign('cate',json_encode($cate));
		return view();
	}
	/**
	 * #素材添加、编辑#
	 */
	function add($id=0){
		if(request()->isAjax()||request()->isPost()){
			$uid=$this->user['id'];
			if($id>0){
			    $has=model($this->table)->read(['id'=>$id,'uid'=>$uid]);
			    //该项不存在或不属于该用户 做添加操作
			    if(empty($has)){
			        $id=0;
			    }
			}
			$options=input('options/a',[]);
			$images=input('images/a',[]);
			$data=[
				'id'=>$id,
				'title'=>input('title/s',''),
				'status'=>input('status/d',1),
				'cate_id'=>input('cate_id/d',1),
				'remark'=>input('remark/s',''),
			];
			$validate=$this->validate($data,[
				'title|素材标题'=>'require',
				'cate_id|素材分组'=>'require',
			]);
			if(true!==$validate){
				$this->error($validate);
			}
			if(isset($options['map'])){
				$options['map']=json_encode($options['map']);
			}
			$data=array_merge($data,$options);
			$operation=false;
			if($data['id']>0){
				$operation=model($this->table)->edit($data);
			}else{
				$data['uid']=$uid;
				$data['s_time']=time();
				$operation=model($this->table)->add($data);
			}
            $source_id=$data['id'];
            if(isset($operation['id'])){
            	$source_id=$operation['id'];
            }
            foreach ($images as $k => $v) {
            	$images[$k]=['id'=>$v,'source_id'=>$source_id];
            }
            // 绑定媒体文件
            if(count($images)){
            	model('media')->edit($images);
            }
            $this->success(lang('Successful operation'));
		}
	}
	/**
	 * #素材删除#
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	function del($id=0){
		if(request()->isAjax()||request()->isPost()){
			$ids=input('ids/a',[]); // 多项删除
	        if($id<=0&&empty($ids)){
	            $this->error(lang('Data does not exist'));
	        }
			$uid=$this->user['id'];
			$where=['id'=>$id,'uid'=>$uid];
			$map=['source_id'=>$id];
			if(!empty($ids)){
				$where=[
					'uid'=>$uid,
					'id'=>['id','in',$ids]
				];
				$map=[
					'source_id'=>['source_id','in',$ids]
				];
			}
	        $del=model($this->table)->del($where);
	        if($del){
	            // 媒体删除
				$this->delImage(0,$map);
	            $this->success(lang('Successful operation'));
	            die;
	        }
	        $this->error(lang('Failed operation'));
	    }
    }
	/**
	 * 获取管理媒体的素材
	 * @param  array  $where 条件
	 * @return json
	 */
	function getSources($cate_id=0,$keyword='',$where=[]){
		$options=[
			'with'=>'media',
		];
		if($cate_id>0){
			$where['cate_id']=$cate_id;
		}
		if($keyword){
			$where['title']=['title','like',"'%$keyword%'"];
		}
		$source=model('source')->readAll($where,true,$options);
		foreach ($source as $k => $v) {
			if(isset($v['map'])){
				$source[$k]['map']=json_decode($v['map'],true);
			}
		}
		return json_encode($source->toArray());
	}
}